import copy
import datetime
import json
import logging
import time

import moment
import requests
import urllib3
from pyquery import PyQuery as jq
from retry import retry
from termcolor import colored
import moment
import pandas as pd
urllib3.disable_warnings()
logging.basicConfig(
format='[%(asctime)s]%(message)s', level=logging.INFO)
Loger = logging.getLogger('zhihu')

def success(txt): return Loger.info(f"{colored(txt, 'green')}")
def info(txt): return Loger.info(f"{colored(txt, 'blue')}")





def warning(txt): return Loger.info(f"{colored(txt, 'yellow')}")


def error(txt): return Loger.info(f"{colored(txt, 'red')}")


# 代码预估价格600，数据每7000条100
url = "https://weixin.sogou.com/weixin?type=2&ie=utf8&query=%E5%8C%97%E4%BA%AC%E5%A4%A7%E5%AD%A6&tsn=5&ft=2018-10-01&et=2018-11-01&interation=&wxid=&usip=&page=1"
data = ''
mylist = []
PROXYURI = 'http://H450116GZ4756AKD:87DEEF49C2EAA8FF@http-dyn.abuyun.com:9020'
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "zh-CN,zh;q=0.9",
"Connection": "keep-alive",
"Host": "weixin.sogou.com",
"Referer": "https://weixin.sogou.com/weixin",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"
}

#关键字列表
keylist = ['大学','大学生','高教','高等教育','高校','学院','学校']
# keylist = ['大学']
datacol = {0:'时间',1:'关键词',2:'标题',3:'来源（公众号名称）',4:'文章url',5:'时间戳'}
class weixi():
    def __init__(self):
        self.mylist = []
        self.nums = 0
        self.index_num = 1
        self.url = "https://weixin.sogou.com/weixin?type=2&ie=utf8&tsn=5&interation=&wxid=&usip="
        self.rzurl = "https://weixin.sogou.com/weixin?type=1&s_from=input&ie=utf8&_sug_=n&_sug_type_=&query="
    def mainSelf(self):
        try:
            for i in range(len(keylist)):
                self.start(keylist[i])
                self.index_num+=1
        finally:
            endlist = sorted(self.mylist, key=lambda item: item[5])
            with open('wx_search.json', 'w', encoding='utf-8') as f:
                f.write(json.dumps(endlist,ensure_ascii=False))
            df = pd.DataFrame(endlist)
            df.rename(columns=datacol,inplace=True)
            df.to_excel('wx_search.xlsx')
    def start(self,strs):
        timelist = list(self.timeSections('2016-01-01', '2018-12-31', None, 24 * 10, 'YYYY-MM-DD'))
        info(timelist)
        for times in timelist:
            for i in range(1, 11):
                myurl = f'{self.url}&page={i}&ft={times[0]}&et={times[1]}&query={strs}'
                warning(myurl)
                self.getContent(myurl,strs)


    @retry()
    def getContent(self, urls,strs):
        resp = requests.get(urls, data=data, verify=False, headers=headers, timeout=5,proxies={'https': PROXYURI,'http': PROXYURI})
        resp.encoding = 'utf-8'
        if '您的访问过于频繁' in resp.text:
            raise ValueError
        else:
            for aa in jq(resp.text)('#main > div.news-box > ul > li').items():
                title = aa('.txt-box > h3').text()
                titleurl = aa('.txt-box > h3 >  a').attr('href')
                wx_gzh = aa('.txt-box > .s-p > a').text()
                times = aa('.txt-box > .s-p').attr('t')
                datetime = moment.date(
                    aa('.txt-box > .s-p').attr('t')).format('YYYY-MM-DD HH:mm:ss')
                # datas = {"title": title, "url": titleurl, "gxh": wx_gzh,"datetime": datetime}
                datas = [datetime,strs,title,wx_gzh,titleurl,times]
                self.nums+=1
                success(f'[{self.nums}] {datetime}: {title}')
                self.mylist.append(datas)

    def timeSections(self, starttimes, endtimes, sectiondays=30, sectionshours=1, formats=None):
        if not starttimes:
            starttimes = moment.now().replace(minutes=0, seconds=0).add(days=-sectiondays)
        else:
            starttimes = moment.date(starttimes)
        if not endtimes:
            endtimes = moment.now().replace(minutes=0, seconds=0)
        else:
            endtimes = moment.date(endtimes)

        while starttimes < endtimes:
            nexttimes = copy.deepcopy(starttimes).add(hours=sectionshours)
            if formats:
                yield starttimes.format(formats), nexttimes.format(formats)
            else:
                yield starttimes, nexttimes
            starttimes = copy.deepcopy(nexttimes).add(hours=24)


if __name__ == '__main__':
    weixi().mainSelf()
    # weixi().start()
